<?php
class Barang_model extends CI_Model {

    var $tabel    = 'barang';

    function __construct() {
        parent::__construct();
		$this->load->library('encrypt');
    }
    
    function get_count($options=array())
    {
		$this->db->like($options);
        $query = $this->db->get($this->tabel);
		return count($query->result_array());
    }

    function get_data()
    {
		//$this->db->like($options);
        $query = $this->db->get($this->tabel);
        return $query->result();
    }
	
	function get_data_join(){
		$sql = "SELECT a.*,b.nmjenis,IFNULL(c.jumlah,0) as jumlah
				FROM barang a
					INNER JOIN jenis b
						ON a.kdjenis=b.kdjenis
					LEFT JOIN (
						SELECT IFNULL(COUNT(idbarang),0) AS jumlah,idbarang
						FROM barangdetail
						GROUP BY idbarang
					) c
						ON a.idbarang=c.idbarang
					";
		$query = $this->db->query($sql);
		return $query->result();
	}

 	function get_data_row($kd){
		$data = array();
		$options = array('idbarang' => $kd);
		$query = $this->db->get_where($this->tabel,$options,1);
		if ($query->num_rows() > 0){
			$data = $query->row_array();
		}

		$query->free_result();    
		return $data;
	}

   function insert_entry()
    {
		$data['idbarang']=$this->input->post('idbarang');
		$data['nmbarang']=$this->input->post('nmbarang');
		$data['partnumber']=$this->input->post('partnumber');
		$data['serialnumber']=$this->input->post('serialnumber');
		$data['berat']=$this->input->post('berat');
		$data['kdjenis']=$this->input->post('kdjenis');
		$data['kdsupplier']=$this->input->post('kdsupplier');
		
		return $this->db->insert($this->tabel, $data);
    }

    function update_entry($kd)
    {
		$data['nmbarang']=$this->input->post('nmbarang');
		$data['partnumber']=$this->input->post('partnumber');
		$data['kdjenis']=$this->input->post('kdjenis');
		$data['kdsupplier']=$this->input->post('kdsupplier');
        
		return $this->db->update($this->tabel, $data, array('idbarang' => $kd));
    }
	

	function delete_entry($kd)
	{
		$this->db->where(array('idbarang' => $kd));

		return $this->db->delete($this->tabel);
	}

}